package com.jz.community.moduleshopping.shopCart.ui;

import android.content.Context;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.utils.LoggerUtils;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.shopCart.adapter.ShopCartBalanceAdapter;
import com.jz.community.moduleshopping.shopCart.bean.CartGoodInfo;
import com.jz.community.moduleshopping.shopCart.bean.NewCartListInfo;

import java.util.List;

/**
 * Created by Tony.Fan on 2018/5/21 14:11
 * <p>
 * 结算弹框
 */
public class ShopCartBalanceDialog implements View.OnClickListener {
    private Context mContext;
    private RecyclerView shopCartBalanceRv;
    private ImageView closeIv;
    private View view;
    private BottomSheetDialog bottomSheetDialog;
    private List<CartGoodInfo> mCartInfosBeans;
    private ConfirmOrderListener confirmOrderListener;

    public void setConfirmOrderListener(ConfirmOrderListener confirmOrderListener) {
        this.confirmOrderListener = confirmOrderListener;
    }

    public ShopCartBalanceDialog(Context context) {
        mContext = context;
        initView();
        addListener();
    }

    public void addCartInfos(List<CartGoodInfo> cartInfosBeans) {
        mCartInfosBeans = cartInfosBeans;
        handleBindAdapter();
        showPopUp();
    }

    private void addListener() {
        closeIv.setOnClickListener(this);
    }

    public void showPopUp() {
        if (bottomSheetDialog == null) {
            bottomSheetDialog = new BottomSheetDialog(mContext);
            bottomSheetDialog.setContentView(view);
        }
        bottomSheetDialog.show();
    }

    public void initView() {
        view = LayoutInflater.from(mContext).inflate(R.layout.shop_cart_balance_layout, null);
        shopCartBalanceRv = view.findViewById(R.id.shop_cart_balance_rv);
        closeIv = view.findViewById(R.id.close_iv);
    }

    private void handleBindAdapter() {
        LinearLayoutManager main_layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        shopCartBalanceRv.setLayoutManager(main_layoutManager);
        ShopCartBalanceAdapter shopCartBalanceAdapter = new ShopCartBalanceAdapter(R.layout.shop_cart_balance_item_layout, mCartInfosBeans);
        shopCartBalanceAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);//加载动画
        shopCartBalanceAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.balance_tv) {
                    confirmOrderListener.toConfirmOrder(shopCartBalanceAdapter.getData().get(position).getCartType(), shopCartBalanceAdapter.getData().get(position).getInPos());
                    bottomSheetDialog.dismiss();
                }
            }
        });
        shopCartBalanceAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                confirmOrderListener.toConfirmOrder(shopCartBalanceAdapter.getData().get(position).getCartType(), position);
                bottomSheetDialog.dismiss();
            }
        });
        shopCartBalanceRv.setAdapter(shopCartBalanceAdapter);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.close_iv) {
            bottomSheetDialog.dismiss();
        }
    }

    public interface ConfirmOrderListener {
        void toConfirmOrder(String cartType, int position);
    }

}
